var add_submit = function(){
	// 再根据条件显示
	var div;
	var sel = DATA_PROD_TYPE[$("#PROD_TYPE").val()];
	switch(sel){
		case "IDC":
			div = "#page_idc";
			break;
		case "专线":
			div = "#page_line";
			break;
		case "集团电话":
			div = "#page_group";
			break;
		case "物联网":
			div = "#page_internet";
			break;
		default:
			break;
	}
	
	var passUrlObj = {
		"id":$("#id").val(),
		"PROD_TYPE":$("#PROD_TYPE").val(),
		"OPERATOR":$("#OPERATOR").val(),
		"group_name":$("#group_name").val(),
		"groupNo":$("#group_no").val(),
		"k1":$(div + " #k1").val(),
		"k2":$(div + " #k2").val(),
		"k3":$(div + " #k3").val(),
		"k4":$(div + " #k4").val(),
		"k5":$(div + " #k5").val(),
		"k6":$(div + " #k6").val(),
		"k7":$(div + " #k7").val(),
		"k8":$(div + " #k8").val()
	};
	var CUR_PROD_TYPE = DATA_PROD_TYPE_GROUP[passUrlObj.PROD_TYPE]

	// var params = passUrlObj;
	var params = {
		"requestData":
			"<OPER_TYPE>UPDATE</OPER_TYPE>"+
			"<OPER_TIME>"+dateFormat(new Date(), "YYYY/MM/DD hh:mm:ss")+"</OPER_TIME>"+
			"<CUST_CODE>"+passUrlObj.groupNo+"</CUST_CODE>"+
			"<OPERATOR>"+passUrlObj.OPERATOR+"</OPERATOR>"+
			"<PROD_TYPE>"+passUrlObj.PROD_TYPE+"</PROD_TYPE>"+
			"<PROD_ID>"+passUrlObj.id+"</PROD_ID>"+
			"<PROD_INFO><"+CUR_PROD_TYPE[0]+">"+
			"<"+CUR_PROD_TYPE[1]+">"+passUrlObj.k1+"</"+CUR_PROD_TYPE[1]+">"+
			"<"+CUR_PROD_TYPE[2]+">"+passUrlObj.k2+"</"+CUR_PROD_TYPE[2]+">"+
			"<"+CUR_PROD_TYPE[3]+">"+passUrlObj.k3+"</"+CUR_PROD_TYPE[3]+">"+
			"<"+CUR_PROD_TYPE[4]+">"+passUrlObj.k4+"</"+CUR_PROD_TYPE[4]+">"+
			"<"+CUR_PROD_TYPE[5]+">"+passUrlObj.k5+"</"+CUR_PROD_TYPE[5]+">"+
			"<"+CUR_PROD_TYPE[6]+">"+passUrlObj.k6+"</"+CUR_PROD_TYPE[6]+">"+
			"<"+CUR_PROD_TYPE[7]+">"+passUrlObj.k7+"</"+CUR_PROD_TYPE[7]+">"+
			"<"+CUR_PROD_TYPE[8]+">"+passUrlObj.k8+"</"+CUR_PROD_TYPE[8]+">"+
			"</"+CUR_PROD_TYPE[0]+"></PROD_INFO>"
	};

	$.ajax({
		type: "post",
		contentType: "application/json",
		data: JSON.stringify(params),
		// url: "../../../opponent/editIdc",
		url: Constants.restHost + "rest/opp/compProdChg",
		dataType: "json",
		async: true,
		headers: {
      'token': getParamValue('token'),
      'saleType': getParamValue('saleType')
    },
    success: function(data) {
      if (data.ResultCode === 1009) {
        alert('登陆超时，请重新登录！');
        jumpToSignin();
        return;
      }
			console.log(data);
			if(data.rtnCode!=1000){
				showErrorMsg(data.rtnMsg);
				console.log("修改失败！code:"+data.rtnMsg);
				return;
			}
			if(data.return.HEADER.RESPONSE.CODE != 0000) {
				showErrorMsg(data.return.HEADER.RESPONSE.DESC);
				console.log("提交失败！code:"+data.return.HEADER.RESPONSE.CODE);
				return;
			}
			console.log("消息:"+data.return.RESULT.RETURN_MSG);

			$("#submit_success").click();
		},
		error : function(textStatus) {
			showErrorMsg("修改数据失败！服务器没有返回。");
			disabled_submit_button(false);
			console.log("修改数据失败！服务器没有返回。");
		}
	});
}
var showErrorMsg = function(msg){
	$("#err_msg_info").html(msg);
	$("#submit_error").click();
}

var hideDiv = function(){
	// 全部隐藏
	$("#page_idc").hide();
	$("#page_line").hide();
	$("#page_group").hide();
	$("#page_internet").hide();
}

var showOpponentData = function(data){
	if(data&&data.PROD_DETAIL&&data.PROD_DETAIL[0]) {
		if(data.PROD_DETAIL[0].START_DATE) {
			data.PROD_DETAIL[0].START_DATE = dateFormat(data.PROD_DETAIL[0].START_DATE, "YYYY-MM-DD");
		}
		if(data.PROD_DETAIL[0].END_DATE) {
			data.PROD_DETAIL[0].END_DATE = dateFormat(data.PROD_DETAIL[0].END_DATE, "YYYY-MM-DD");
		}
  	}
	hideDiv();
	//var data = JSON.parse(decodeURIComponent(getParamValue("idcdata")));
	
	// 初始化START
	var div;
	var prodType = DATA_PROD_TYPE[data.PROD_TYPE];
	switch(prodType){
		case "IDC":
			div = "#page_idc";
			$("#page_idc").show();
			break;
		case "专线":
			div = "#page_line";
			$("#page_line").show();
			break;
		case "集团电话":
			div = "#page_group";
			$("#page_group").show();
			break;
		case "物联网":
			div = "#page_internet";
			$("#page_internet").show();
			break;
		default:
			break;
	}
	
	$("#id").val(data.PROD_ID);
	$("#group_name").val(decodeURIComponent(getParamValue("group_name")));
	$("#group_no").val(data.CUST_CODE);
	/**
	* 解决AmazeUI在IOS下，JS动态给select赋值时出现多选的情况
	* 使用方法：
	* $q(selector).val();
	*/
	(function (w) {
		var Select = function(selector) {
			this.selector = selector;
			return new Select.prototype.init(selector);
		}
		Select.prototype = {
			constructor : Select,
			init: function(selector) {
				this.$select = $(selector);
			},
			val: function(val) {
				var $select = this.$select;
				$.each($select.siblings().find(".am-selected-list li"), function(i, option) {
					$(option).removeClass("am-checked");
					if(""+$(option).data("value") === ""+val) {
						$select.val(val);
						$(option).addClass("am-checked");
						$select.siblings().find(".am-fl").text($(option).find(".am-selected-text").text());
					}
				});
			}
		}
		Select.prototype.init.prototype = Select.prototype;
		w.$q = Select;
	})(window);

	// $("#PROD_TYPE").find("option[value='"+data.PROD_TYPE+"']").attr("selected",true);
	$q("#PROD_TYPE").val(data.PROD_TYPE);
	// $("#OPERATOR").find("option[value='"+data.OPERATOR+"']").attr("selected",true);
	$q("#OPERATOR").val(data.OPERATOR);
	if(prodType === "专线") {
		// $(div + " #k1").find("option[value='"+data.PROD_DETAIL[0].BUSI_TYPE+"']").attr("selected",true);
		$q(div + " #k1").val(data.PROD_DETAIL[0].BUSI_TYPE);
	} else if(prodType === "物联网"){
		// $(div + " #k1").find("option[value='"+data.PROD_DETAIL[0].NET_TYPE+"']").attr("selected",true);
		$q(div + " #k1").val(data.PROD_DETAIL[0].NET_TYPE);
	}else{
		$(div + " #k1").val(data.PROD_DETAIL[0][DATA_PROD_TYPE_GROUP[data.PROD_TYPE][1]]);
	}
	$(div + " #k2").val(data.PROD_DETAIL[0][DATA_PROD_TYPE_GROUP[data.PROD_TYPE][2]]);
	$(div + " #k3").val(data.PROD_DETAIL[0][DATA_PROD_TYPE_GROUP[data.PROD_TYPE][3]]);
	$(div + " #k4").val(data.PROD_DETAIL[0][DATA_PROD_TYPE_GROUP[data.PROD_TYPE][4]]);
	$(div + " #k5").val(data.PROD_DETAIL[0][DATA_PROD_TYPE_GROUP[data.PROD_TYPE][5]]);
	$(div + " #k6").val(data.PROD_DETAIL[0][DATA_PROD_TYPE_GROUP[data.PROD_TYPE][6]]);
	$(div + " #k7").val(data.PROD_DETAIL[0][DATA_PROD_TYPE_GROUP[data.PROD_TYPE][7]]);
	$(div + " #k8").val(data.PROD_DETAIL[0][DATA_PROD_TYPE_GROUP[data.PROD_TYPE][8]]);
	// 初始化END
	
	// 显示 隐藏 新增界面
	$("#PROD_TYPE").on('change', function() {
		// 先全部隐藏
		hideDiv();
		
		// 再根据条件显示
		var sel = DATA_PROD_TYPE[($("#PROD_TYPE").val())];
		switch(sel){
			case "IDC":
				$("#page_idc").show();
				break;
			case "专线":
				$("#page_line").show();
				break;
			case "集团电话":
				$("#page_group").show();
				break;
			case "物联网":
				$("#page_internet").show();
				break;
			default:
				break;
		}
	});
};



var back_list = function(){
	window.location.href = "javascript:history.go(-1);";
}
var disabled_submit_button = function(open){
		if(open){
			$("#submit_wite").click();
			$("#add_submit").html(" <img width='20px;' src='../../../img/progress.gif'> 提交中...");
			$("#add_submit").attr("disabled","disabled");
		}else{
			$("#wite_close").click();
			$("#add_submit").html("提交");
			$("#add_submit").removeAttr("disabled");
		}
};

$(function(){
	$('.date-input').on({
		'keyup': function(e) {
			var dateInput = $(this).val();
			if(e.keyCode >= 48 && e.keyCode <= 57 || e.keyCode >= 96 && e.keyCode <= 105) {
				if(dateInput.length === 4) {
					$(this).val(dateInput + '-');
				}
				if(dateInput.length === 7) {
					$(this).val(dateInput + '-');
				}
			} else if(e.keyCode === 8) {
				if(dateInput.length === 8) {
					$(this).val(dateInput.replace(/[-]$/, ""));
				}
				if(dateInput.length === 5) {
					$(this).val(dateInput.replace(/[-]$/, ""));
				}
			}
		}
	});
	DATA_OPERATORS = ["", "联通", "电信"],
    DATA_PROD_TYPE = ["", "IDC", "专线", "集团电话", "物联网"],
    DATA_PROD_TYPE_GROUP = [
    	[],
    	["IDC_INFO","INT_BANDWIDTH","MACHINE_ROOM","RACK_NUM","RACK_POWER","COST","START_DATE","END_DATE","AMOUNT"],
    	["LINE_INFO","BUSI_TYPE","BANDWIDTH","ARTI_NUM","COST","START_DATE","END_DATE","AMOUNT","AVE_TRAFFIC"],
    	["TEL_INFO","ACCESS","INSTALLED_NUM","INSTALLED_ADDR","SEG_NO","COST","START_DATE","END_DATE","AMOUNT"],
    	["IOT_INFO","NET_TYPE","BUSI_TYPE","APP_SCENE","PACK_COST","COST","START_DATE","END_DATE","AMOUNT"]
    ];
    id = getParamValue("id");
 	var groupNo = getParamValue("group_no"),
        operator = decodeURIComponent(getParamValue("operator")),
        prod_type = decodeURIComponent(getParamValue("prod_type"));
    console.log("id="+id);

    params = {
      "requestData":"<CUST_CODE>"+groupNo+"</CUST_CODE>"+
      "<OPERATOR>"+DATA_OPERATORS.indexOf(operator)+
      "</OPERATOR><PROD_TYPE>"+DATA_PROD_TYPE.indexOf(prod_type)+"</PROD_TYPE>"+
      "<PROD_ID>"+id+"</PROD_ID>"
    };

    $.ajax({
      type: "post",
      contentType: "application/json",
      data: JSON.stringify(params),
      // url: "../../../opponent/getIdcDetailsById?id=" + id,
      url: Constants.restHost + "rest/opp/compProdQryDetail",
      dataType: "json",
      async: true,
      headers: {
      'token': getParamValue('token'),
      'saleType': getParamValue('saleType')
    },
    success: function(data) {
      if (data.ResultCode === 1009) {
        alert('登陆超时，请重新登录！');
        jumpToSignin();
        return;
      }
        if (data.rtnCode != 1000) {
          console.log("接口返回失败！code:" + data.rtnMsg);
          return;
        }
        $("#modal-loading").modal('close');
        showOpponentData(data.return.RESULT);
      },
      error: function(textStatus) {
        $("#modal-loading").modal('close');
        showBottomMsg("获取信息失败！");
        console.info("request error:");
      }
    });
  });
